// pages/my/edit-user-info/edit-user-info.js
import WxValidate from '../../../utils/WxValidate'
const app = getApp()
const api = require('../../../utils/util')
Page({
    // 页面分享
    onShareAppMessage () {},
    // 分享朋友圈
    onShareTimeline () {},

  /**
   * 页面的初始数据
   */
  data: {

    form: {
      avatar: '',
      name: '',
      phone: '',
      email: '',
      companyName: '',
      headpic: ''
    },
    siteUrl: app.globalData.siteUrl

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
      this.initValidate();
      let userinfo = app.globalData.userinfo;
      console.log(userinfo)
      if(!userinfo){
         wx.navigateTo({
           url: '/pages/login/login',
         })
      }

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let that = this;
    let form = that.data.form;
    form.name = app.globalData.userinfo.name;
    if(app.globalData.userinfo.headpic){
      form.avatar = app.globalData.siteUrl + app.globalData.userinfo.headpic;
    }else{
      form.avatar = '';
    }
    form.headpic = app.globalData.userinfo.headpic;
    form.email = app.globalData.userinfo.email;
    form.phone = app.globalData.userinfo.usermobile;
    that.setData({
      form
     })

  },

  // 初始化表单验证
  initValidate() {
    //验证规则
    const rules = {
      name: {
        required: true
      },
      phone: {
        required: true,
        tel: true
      },
      email: {
        email: true
      }
    };
    //验证规则提示语，若不传则调用默认的信息
    const messages = {
      name: {
        required: "请输入您的姓名"
      },
      phone: {
        required: "请输入您的联系电话",
        tel: "请输入正确的联系电话"
      },
      email: {
        email: '请输入正确的邮箱号'
      }
    };

    this.WxValidate = new WxValidate(rules, messages)
  },
  // 通用输入函数
  inputHandle(e) {
    let { name } = e.currentTarget.dataset;
    let value = e.detail;
    let form = this.data.form;
    form[name] = value.trim();
    this.setData({
      form
    });
  },

  // 提交
  submitHandle() {
    let {
      form
    } = this.data;
    // 表单验证
    if (!this.WxValidate.checkForm(this.data.form)) {
      const error = this.WxValidate.errorList[0];
      wx.showToast({
        title: error.msg,
        icon: 'none',
        image: '',
        duration: 1500,
        mask: false,
        success: (result)=>{
          
        },
        fail: ()=>{},
        complete: ()=>{}
      });
      return false
    }
    let param = {
      id: app.globalData.userinfo.id,
      headpic: form.headpic,
      name: form.name,
      usermobile: form.phone,
      email: form.email
    }
    console.log(param)
    api.edit_enterprise_member(param).then(function(res){
      if(res.code == 0){
        app.globalData.userinfo = res.data;
      }
      wx.switchTab({
               url: '/pages/my/my',
      })
    })
  },

  //上传图像
  afterRead(event) {
    const that = this;
    const { file } = event.detail;
    let {
      form
    } = this.data;
    var header = { 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8' };
    wx.uploadFile({
      url: app.globalData.uploadUrl, 
      filePath: file.url,
      name: 'file',
      header:header,
      formData: { user: 'test' },
      success(res) {
        var api_data = JSON.parse(res.data) 
        // 上传完成需要更新 fileList
        form.headpic = api_data.data.pic;
      },
    });
    
    form.avatar = file.url;
    this.setData({
      form
    });
  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})
